<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends Controller {
    public $GoodsModel;
    public function __construct(){
        parent::__construct();
        $this->GoodsModel = D('Goods');
    }
    public function goodsadd(){
        if(IS_POST){
            $upload = new \Think\Upload();// 实例化上传
            $upload->maxSize   =     5*1024*1024;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =      './Upload/'; // 设置附件上传根目录
            $info   =   $upload->upload();
            if(!$info){
                 $this->error($upload->getError());
            }


            $_POST['goods_img']='./Upload/'.$info['goods_img']['savepath'].$info['goods_img']['savename'];

            $image = new \Think\Image();
            $image->open($_POST['goods_img']);
            // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
            $_POST['thumb_img'] = './Upload/thumb/'.$info['goods_img']['savename'];
            $image->thumb(150, 150)->save($_POST['thumb_img']);

            if(!$this->GoodsModel->create()){
                echo $this->GoodsModel->getError();
            }else {
                $addinfo = $this->GoodsModel->add();

                if($addinfo){
                    $this->success('添加成功','goodslist',3);
                }else {
                    $this->error('hahaha');
                }
            }

        }else {
            $this->display();
        }

    }
    public function goodslist(){
        if(!$_GET['p']){
            $_GET['p'] = 1;
        }
        $goodslist = $this->GoodsModel->cache(true)->page($_GET['p'].',10')->select();
        $count = $this->GoodsModel->count();
        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $this->assign('show',$show);
        $this->assign('goodslist',$goodslist);
        $this->display();
    }

    public function goods_del(){
        if($this->GoodsModel->delete(I('get.goods_id'))){
            $this->redirect('Admin/goods/goodslist');
        }else{
            echo '翻滚吧，牛宝宝';
        }
    }
}
